﻿using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace Dgg.LearnYourTools.NUnit.C_Assertions.e_ChangeBehavior
{
	[TestFixture]
	public class _027_AlternativeEquality
	{
		[Test]
		public void AlternativeEquality()
		{
			Version onePointOh = new Version(1, 0), onePointOhPointFive = new Version(1, 0, 5);

			Assert.That(onePointOh, Is.Not.EqualTo(onePointOhPointFive));

			Assert.That(onePointOh, Is.EqualTo(onePointOhPointFive).Using(new MajorAndMinorEquality()));
		}

		class MajorAndMinorEquality : IEqualityComparer<Version>
		{
			public bool Equals(Version x, Version y)
			{
				if (!ReferenceEquals(x, y) && (ReferenceEquals(null, y) || ReferenceEquals(x, null))) return false;
				if (ReferenceEquals(x, y)) return true;
				return x.Major.Equals(y.Major) && x.Minor.Equals(y.Minor);
			}

			public int GetHashCode(Version obj)
			{
				unchecked
				{
					return obj != null ? (obj.Major * 397) ^ obj.Minor : 0;
				}
			}
		}
	}
}
